package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.CartService;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartController {

    //   CartController/listCart?userId=12345671111
    //   CartController/listCart?userId=12345671111&businessId=10001
    public Object listCart(HttpServletRequest request){
        List<Cart> list = new ArrayList<>();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));

        if (cart.getBusinessId()!=null){
            cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        }

        CartService cartService = new CartServiceImpl();
        list = cartService.listCart(cart);
        return list;
    }

    //  CartController/saveCart?userId=12345671123&businessId=100002&foodId=2
    public int saveCart(HttpServletRequest request){
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.valueOf(request.getParameter("foodId"));
//        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService cartService = new CartServiceImpl();
        int i = cartService.saveCart(userId,businessId,foodId);
        return i;
    }

    //  CartController/updateCart?quantity=30&userId=12345671123&businessId=100002&foodId=2
    public int updateCart(HttpServletRequest request){
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        Integer quantity = Integer.parseInt(request.getParameter("quantity"));
        CartService cartService = new CartServiceImpl();
        int i = cartService.updateCart(userId,businessId,foodId,quantity);
        return i;
    }

    //  CartController/removeCart?userId=12345671112&businessId=100003
    public int removeCart(HttpServletRequest request){
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        CartService cartService = new CartServiceImpl();
        int i = cartService.removeCart(userId, businessId, foodId);
        return i;
    }

}
